<?php

class WidgetController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }
	public function indexAction(){
		$widgetModel=new Model_Widget();
		$widget=$widgetModel->fetchAllWidgets();
		$this->view->widgetList=$widget;
	}
	public function createAction(){
		$frmWidget=new Form_WidgetForm();
		$frmWidget->setMethod('post');
		$frmWidget->setAction($this->getRequest()->getBaseUrl().'/widget/create');
		if($this->getRequest()->isPost() && $frmWidget->isValid($_POST)){
			$widgetModel=new Model_Widget();
			$widgetModel->createWidget($_POST['title'],$_POST['content']);
			$this->_forward('index');
		}
		$this->view->form=$frmWidget;
	}
	public function editAction(){
		$frmWidget=new Form_WidgetForm();
		$widgetModel=new Model_Widget();
		$id=$this->getRequest()->getParam('id');	
		if($this->getRequest()->isPost() && $frmWidget->isValid($_POST)){
			$widgetModel->updateWidget($_POST['id'],$_POST['title'],$_POST['content']);
			$this->_forward('index');
		}	
		$widget=$widgetModel->fetchWidget($id);
		if($widget){			
			$element=new Zend_Form_Element_Hidden('id');
			$element->setValue($id);
			$frmWidget->getElement('title')->setValue($widget->title);
			$frmWidget->getElement('content')->setValue($widget->content);
			$frmWidget->addElement($element);
			$frmWidget->setMethod('post');
			$frmWidget->setAction($this->getRequest()->getBaseUrl().'/widget/edit');
			$this->view->form=$frmWidget;
		}else{
			$this->_forward('index ');
		}				
	}
	public function deleteAction(){
		$id=$this->getRequest()->getParam('id');
		$optionModel=new Model_Option();
		$option=$optionModel->fetchOption('homepage_widgets');
		if($option){
			$widgetIds=explode(',',$option->value);
			foreach($widgetIds as $key=>$idd){
				if($idd==$id){
					unset($widgetIds[$key]);
				}
			}
			$widgetIds=implode(',',$widgetIds);
			$optionModel->updateOption('homepage_widgets',$widgetIds);
		}
		$option=$optionModel->fetchOption('linklist_widgets');
		if($option){
			$widgetIds=explode(',',$option->value);
			foreach($widgetIds as $key=>$idd){
				if($idd==$id){
					unset($widgetIds[$key]);
				}
			}
			$widgetIds=implode(',',$widgetIds);
			$optionModel->updateOption('linklist_widgets',$widgetIds);
		}
		$widgetModel=new Model_Widget();
		$widget=$widgetModel->deleteWidget($id);
		$this->_forward('index ');
	}
	public function addAction(){
		$id=$this->getRequest()->getParam('id');
		$sidebar=$this->getRequest()->getParam('sidebar');
		$widgetModel=new Model_Widget();
		if($sidebar=='homepage_widgets'){
			$widgetModel->addToHomePage($id);
		}else if($sidebar=='linklist_widgets'){
			$widgetModel->addToLinkList($id);
		}
		$this->_forward('index ');
	}
	public function removeAction(){
		$id=$this->getRequest()->getParam('id');
		$sidebar=$this->getRequest()->getParam('sidebar');
		$widgetModel=new Model_Widget();
		if($sidebar=='homepage_widgets'){
			$widgetModel->removeFromHomePage($id);
		}else if($sidebar=='linklist_widgets'){
			$widgetModel->removeFromLinkList($id);
		}
		$this->_forward('index ');
	}
}
?>